package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author caiwei
 * @date 2020-8-23 21:59
 */
public class XMLMapperBuilder {
    private Configuration configuration;
    public XMLMapperBuilder(Configuration configuration) {
        this.configuration=configuration;
    }

    public Configuration parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        putMappedStatementMap(list,namespace);

        List<Element> insertList = rootElement.selectNodes("//insert");
        putMappedStatementMap(insertList,namespace);

        List<Element> updateList = rootElement.selectNodes("//update");
        putMappedStatementMap(updateList,namespace);

        List<Element> deleteList = rootElement.selectNodes("//delete");
        putMappedStatementMap(deleteList,namespace);
        return configuration;
    }

    private void putMappedStatementMap(List<Element> list,String namespace){
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setSql(sqlText);
            String key=namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

}
